package com.jcark.ui

import android.animation.LayoutTransition
import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.style.DynamicDrawableSpan
import android.text.style.ImageSpan
import android.text.style.RelativeSizeSpan
import android.util.Log
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.get
import com.blankj.utilcode.util.SizeUtils
import com.blankj.utilcode.util.TimeUtils
import com.jakewharton.rxbinding4.view.clicks
import com.jcark.ui.dialog.DialogUtils
import com.jcark.ui.dialog.MutableStyleDialog
import com.jcark.ui.dialog.style.BlueStyle
import com.jcark.ui.font.FontManager
import com.jcark.ui.graphics.style.ShadowDrawableHelper
import com.jcark.ui.utils.ViewUtils
import com.jcark.ui.view.bottomnavigationview.MBottomNavigationView
import com.jcark.ui.view.magicindicator.MagicIndicatorHelper
import com.jcark.ui.view.magicindicator.OnTabClickListener
import com.jcark.ui.view.span.JImageSpan
import com.jcark.ui.view.textview.CollapsedTextView
import com.jcark.ui.view.textview.MTextView
import com.jcark.ui.widget.datepicker.DatePickerPopupWindow
import com.jcark.ui.widget.datepicker.DatePickerUtils
import com.jcark.ui.widget.datepicker.DateTimeParameter
import com.jcark.ui.widget.datepicker.MTimePickerView
import com.jcark.utilscode.MToastUtils
import com.jcark.utilscode.ScreenSizeUtils
import net.lucode.hackware.magicindicator.FragmentContainerHelper
import net.lucode.hackware.magicindicator.MagicIndicator
import java.util.concurrent.TimeUnit


class MainActivity : AppCompatActivity() {

    var mainDialog: MutableStyleDialog? = null
    var blueDialog: MutableStyleDialog? = null
    private var mIsEdit = false
    private var isShow = false
    private var mFragmentContainerHelper: FragmentContainerHelper? = null
    private var mTimePickerView: MTimePickerView? = null
    private var mSelectDate: String? = null
    private var mDatePickerPopupWindow: DatePickerPopupWindow? = null

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val tvTitle = findViewById<MTextView>(R.id.tv_title)
        tvTitle.text = "<成都+九寨沟+黄龙+熊猫基地双飞6日游>牛人自组团 16 常规 线粗0.8"

        val tvDialogMain = findViewById<TextView>(R.id.tvDialogMain)
        tvDialogMain.setOnClickListener {
            dialogMainStyle()
        }

        tvDialogMain.setTypeface(
            FontManager.getInstance().getTypeface(this, FontManager.DIN_MEDIUM)
        )
        tvDialogMain.text = "<成都+九寨沟+黄龙+熊猫基地双飞6日游>牛人自组团"


        findViewById<TextView>(R.id.tvDialogBlue).setOnClickListener {
            dialogBlueStyle()
        }

        findViewById<TextView>(R.id.tvHtmlText).setOnClickListener {
            forJImageSpan()
            //addImageSpan()
            testBottomMenu()
        }
        findViewById<TextView>(R.id.tvCreateDateDialog).setOnClickListener {
            val dateTimeParameter = DateTimeParameter("2025-01-01", "2025-03-30")
            dateTimeParameter.type = booleanArrayOf(true, true, false, false, false, false)
            dateTimeParameter.isBottomDialog = true
            //dateTimeParameter.initDate = "2025-03-10"
            var selectDate: String? = null
            mTimePickerView = DatePickerUtils.createDateTimeDialog(
                this,
                dateTimeParameter,
                { date, _ -> selectDate = TimeUtils.date2String(date, "yyyy-MM-dd") },
                {
                    MToastUtils.shortShow(selectDate ?: "sss")
                },
                {
                    //取消
                }

            )
            mTimePickerView?.show()
        }

        val dateTimeParameter = DateTimeParameter("2025-01-01", "2025-03-30")
        dateTimeParameter.type = booleanArrayOf(true, true, false, false, false, false)
        mDatePickerPopupWindow = DatePickerPopupWindow(
            this, dateTimeParameter
        ) { date, _ ->
            mSelectDate = TimeUtils.date2String(date, "yyyy-MM-dd")
            MToastUtils.shortShow(mSelectDate ?: "sss")
        }

        findViewById<TextView>(R.id.tvCreateDatePicker).setOnClickListener {
            mDatePickerPopupWindow?.showPopupWindow()
        }

        val endDate = "2022.08.29"
        if (endDate.indexOf(".") > 0 && endDate.length == 10) {
            Log.d("KING", endDate.substring(0, 4))
            Log.d("KING", endDate.substring(5, 10))
        }

        val tvCheckNetwork = findViewById<TextView>(R.id.tvCheckNetwork)

        val myString1 = "01234,56789"
        Log.d("KING", "new1===" + myString1.substring(0, myString1.indexOf(",")))
        val myString2 = "0123456789"
        Log.d("KING", "new2===" + myString2.substring(0, myString2.length))


        Thread(Runnable {
            Thread.sleep(5000)
            runOnUiThread {
                val tvRemoveSpace = findViewById<MTextView>(R.id.tv_remove_space_1)
                tvRemoveSpace.text = "一二三四五六七八九如果是多行文字之间的距离文字和"
            }
        }).start()

        /*  val tvRemoveSpace2 = findViewById<MTextView>(R.id.tv_remove_space_2)
          //tvRemoveSpace2.removeSpace()
          tvRemoveSpace2.text="使用的样式是主题样式使用的样式是主题样式使用的样式是主题样式使用的样式是主题样式使"*/

        val tvEditView = findViewById<TextView>(R.id.tv_edit)
        tvEditView.setOnClickListener {
            changeRightViewText(tvEditView)
        }

        val showHiddenLayout = findViewById<LinearLayout>(R.id.layout_show_hidden)

        val layoutHeight = showHiddenLayout.height
        val lt = LayoutTransition()
        val tvMessage = findViewById<TextView>(R.id.tv_show_message)
        lt.disableTransitionType(LayoutTransition.DISAPPEARING)
        //showHiddenLayout.layoutTransition= lt
        findViewById<TextView>(R.id.tv_show_hidden).setOnClickListener {
            //AnimationUtils.showOrHideAnimation(showHiddenLayout, AnimationState.STATE_HIDE_UP, 1000)

            if (isShow) {
                tvMessage.visibility = View.VISIBLE
                tvMessage.animation = AnimationUtils.loadAnimation(this, R.anim.dd_menu_in)
                //AnimationUtils2.visibleAnimator(showHiddenLayout, layoutHeight)
            } else {

                tvMessage.visibility = View.GONE
                tvMessage.animation = AnimationUtils.loadAnimation(this, R.anim.dd_menu_out)

                //showHiddenLayout.visibility = View.GONE
                //AnimationUtils2.invisibleAnimator(showHiddenLayout, layoutHeight)
            }

            isShow = !isShow

            /*    if(isShow){
                    AnimationUtils2.changeViewHeightAnimatorStart(showHiddenLayout,showHiddenLayout.height,0)
                }else{
                    AnimationUtils2.changeViewHeightAnimatorStart(showHiddenLayout,0,layoutHeight)
                }*/

        }
        testShadow()
        testTabs()
        showMoreTest()


        tvMessage.clicks().throttleFirst(3000, TimeUnit.MILLISECONDS).subscribe {
            Log.d("KING", "======防抖动=======")
        }

        bindTitle()
    }

    fun setOnClickListeners(vararg target: View) {
        for (view in target) {
            view.clicks().throttleFirst(3000, TimeUnit.MILLISECONDS).subscribe {

            }
        }
    }

    private fun bindTitle() {
        var imageResId = R.mipmap.ic_route_group_18
        val tvBasicTitle = findViewById<CollapsedTextView>(R.id.tv_basic_title)

        tvBasicTitle.setLeftDrawableResourceId(imageResId)

        tvBasicTitle.text = "  " + "熊猫基地双飞6日游>牛人自组团2"
        /*val params = tvBasicTitle.layoutParams ?: ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )*/

        val screenWidth = ScreenSizeUtils.getScreenWidth(this)
        val titleWidth = screenWidth - SizeUtils.dp2px(48f)
        //params.height = 500
        //tvBasicTitle.layoutParams = params
        Log.d(
            "KING",
            "==MMMMMMMMMM===lineH==${tvBasicTitle.lineHeight}....Height==${tvBasicTitle.height}...screenWidth=$screenWidth...titleWidth=$titleWidth"
        )
    }

    private fun testShadow() {
        val tvShadowView = findViewById<TextView>(R.id.tv_shadow)
        val config: ShadowDrawableHelper.Builder = ShadowDrawableHelper.Builder()
            .setColor(ContextCompat.getColor(this, R.color.main_color)) //View颜色
            .setShadowColor(ContextCompat.getColor(this, R.color.gray)) //阴影颜色
            //.setGradientColorArray(mColor) //如果View是渐变色，则设置color数组
            .setRadius(ViewUtils.dpToPx(this, 30f)) //圆角
            .setOffsetX(0) //横向偏移
            .setOffsetY(0) //纵向偏移


        ShadowDrawableHelper.setShadowDrawable(tvShadowView, config)
    }

    private var isShowMenuTitle = true
    private fun testBottomMenu() {
        val bottomMenuView: MBottomNavigationView = findViewById(R.id.main_bottom)

        //bottomMenuView.setTextViewVisible(true)
        for (i in 0 until bottomMenuView.menu.size()) {
            bottomMenuView.menu[i].title = "a$i"
        }
        isShowMenuTitle = !isShowMenuTitle
        if (isShowMenuTitle) {
            // bottomMenuView.setIconVisibility(View.GONE)
            bottomMenuView.setTextVisibility(View.VISIBLE)
        } else {
            bottomMenuView.setTextVisibility(View.GONE)
        }

        // bottomMenuView.setTextVisibility(View.VISIBLE)
    }

    private fun changeRightViewText(textView: TextView) {
        if (mIsEdit) {
            textView.text = "退出管理"
            textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
            textView.compoundDrawablePadding = 0
        } else {
            textView.text = "管理"
            textView.setCompoundDrawablesWithIntrinsicBounds(
                ContextCompat.getDrawable(this, R.mipmap.ic_edit_main),
                null,
                null,
                null
            )
            textView.compoundDrawablePadding = 24
        }
        mIsEdit = !mIsEdit
    }

    private fun dialogMainStyle() {
        if (null == mainDialog) {
            mainDialog = DialogUtils.createDialog(
                this,
                "主题标题",
                "使用的样式是主题样式使用的样式是主题样式使用的样式是主题样式使用的样式是主题样式使用的样式是主题样式使用的样式是主题样式使用的样式是主题样式",
                { mainDialog?.dismiss() },
                { mainDialog?.dismiss() })
        }

        mainDialog?.show()
    }

    private fun dialogBlueStyle() {
        if (null == blueDialog) {
            blueDialog = DialogUtils.createDialog(
                this,
                BlueStyle(),
                "蓝色标题",
                "消息内容消息内容消息内容消息内容消息内容消息内容消息内容消息内容",
                "关闭",
                { blueDialog?.dismiss() },
                "退出",
                { blueDialog?.dismiss() })
        }
        blueDialog?.show()
    }

    private fun forJImageSpan() {
        var htmlTextView = findViewById<MTextView>(R.id.tvHtmlTextResult)
        val str1 =
            "国AFDSLFJSFJSFJLKSFJSJFSFJLSJFKLASJFKLSJFKLDJSFKLJDSKLFJDKLSFJDKLSFJDKLSFJLDKSFKLDS"
        val str2 =
            "只要点击了，请会有惊喜，当然也可能是惊吓...只要点击了，请会有惊喜，当然也可能是惊吓...只要点击了，请会有惊喜，当然也可能是惊吓"
        var text = SpannableString(str1)
        val sizeSpan1 = RelativeSizeSpan(2f)
        var image =
            JImageSpan(this, R.mipmap.ic_route_group_18, DynamicDrawableSpan.ALIGN_CENTER, 0, 50)
        val imageSpan =
            ImageSpan(this, R.mipmap.ic_route_group_18, DynamicDrawableSpan.ALIGN_BASELINE)
        //todo:King.zi 前面必须要添加一个字符
        text.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        htmlTextView.text = text
    }

    private fun showMoreTest() {
        //val conent = "只要点击了，请会有也可能是惊吓.请会有惊喜，当然也可能是惊吓请会有惊喜，当然也可能是惊吓"
        //val conent = " 只要点击了，文桂林5日游【桂【桂林+阳朔+张三李四+五六七八九十+】"
        val conent =
            " <成都+九寨沟+黄龙+熊猫基地双飞6日游>牛人自组团九寨沟+黄龙+熊猫基地双飞6日游>牛人自组团九寨沟+黄龙+熊猫基地双飞6日游>牛人自组团九寨沟+黄龙+熊猫基地双飞6日游>牛人自组团"
        val moreView = findViewById<CollapsedTextView>(R.id.tv_more)
        // moreView.typeface = FontManager.getInstance().getTypeface(this, FontManager.DIN_MEDIUM)
        moreView.setLeftDrawableResourceId(R.mipmap.ic_route_group_18)
        moreView.text = conent
        moreView.setOnClickListener {
            Log.d("KING", "aaaaaaaaaaaaaaaa")
        }
    }

    private fun testTabs() {
        var magicTabs = findViewById<MagicIndicator>(R.id.tabs)
        mFragmentContainerHelper = FragmentContainerHelper(magicTabs)
        MagicIndicatorHelper.createMagicIndicator(
            this,
            null,
            magicTabs,
            mutableListOf("标签1", "标签2"),
            true,
            true,
            16f,
            isPerformClick = false,
            10f,
            10f,
            Color.parseColor("#666666"),
            Color.parseColor("#333333"),
            true,
            null,
            Color.parseColor("#FFD22D"),
            Color.parseColor("#FFFFBB18"), 50f, tabClickListener =
            object : OnTabClickListener {
                override fun onTabClick(position: Int) {
                    mFragmentContainerHelper?.handlePageSelected(position)
                }
            }
        )
    }
}